package org.example.search;

public class InsertSearch {

    public static void main(String[] args) {
        int[] arr = new int[100];
        for (int i=0; i<arr.length; i++){
            arr[i] = i + 1;
        }
        int index = insertSearch(arr,0,arr.length-1,75);
        System.out.println("index=" + index);

    }

    public static int insertSearch(int[] arr,int left,int right,int findVal){

        if (left > right || findVal < arr[0] || findVal > arr[arr.length -1]){
            return -1;
        }
        //自适应求出mid
        int mid = left + (right -left)*(findVal - arr[left])/(arr[right] - arr[left]);
        int midVal = arr[mid];
        if (findVal > midVal){
            return insertSearch(arr,mid +1,right,findVal);
        }else if (findVal < midVal){
            return insertSearch(arr,left,mid -1,findVal);
        }else{
            return mid;
        }


    }

}
